package com.yunbao.hujin.myexplainguide;

import android.content.Intent;
import android.graphics.RectF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;

import com.yunbao.hujin.guidelibrary.HighLight;
import com.yunbao.hujin.guidelibrary.utils.ZLog;

public class MainActivity extends AppCompatActivity {

    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //个人中心
        view = findViewById(R.id.id_btn_important);

        //获取View宽高:OnGlobalLayoutListener可能会被多次触发，因此在得到了高度之后，要将OnGlobalLayoutListener注销掉。
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                addHightView();
            }
        });

        //下一页
        findViewById(R.id.go_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

    }

    private void addHightView() {
        HighLight highLight = new HighLight(this)
                .anchor(findViewById(R.id.id_container))   //绑定根布局，在Activity中可不写（但是除非此Activity继承Activity,否则报ClassCastException:FrameLayout$LayoutParams cannot be cast）
                .setIntercept(true)                         //是否需要拦截点击事件   // 查看注释和代码，可设置其他属性
                .setShadow(false)                           //是否需要模糊化边框
                .setIsNeedBorder(true)                      //是否需要边框
                .setMyBroderType(HighLight.MyType.DASH_LINE)//设置边框类型
                //增加高亮布局 1
                .addHighLight(R.id.id_btn_important, R.layout.main_info_up, new HighLight.OnPosCallback() {
                    @Override
                    public void getPos(float rightMargin, float bottomMargin, RectF rectF, HighLight.MarginInfo marginInfo) {
                        ZLog.e("rectF.right" + rectF.right);
                        ZLog.e("rectF.width()" + rectF.width());
                        ZLog.e("rectF.bottom" + rectF.bottom);
                        ZLog.e("--------------------------------------------------------------------");

                        marginInfo.leftMargin = rectF.right - rectF.width() / 2;
                        marginInfo.topMargin = rectF.bottom;

                        ZLog.e("1. " + marginInfo.leftMargin + "  :  " + marginInfo.topMargin);
                    }
                })
                //增加高亮布局 1
                .addHighLight(R.id.id_btn_amazing, R.layout.main_info_down, new HighLight.OnPosCallback() {
                    /**
                     * @param rightMargin  高亮view在anchor中的右边距
                     * @param bottomMargin 高亮view在anchor中的下边距
                     * @param rectF        高亮view的l,t,r,b,w,h都有
                     * @param marginInfo   设置你的布局的位置，一般设置l,t或者r,b
                     */
                    @Override
                    public void getPos(float rightMargin, float bottomMargin, RectF rectF, HighLight.MarginInfo marginInfo) {
                        ZLog.e("rightMargin" + rightMargin);
                        ZLog.e("rectF.width()" + rectF.width());
                        ZLog.e("rectF.height()" + rectF.height());
                        ZLog.e("bottomMargin" + bottomMargin);
                        ZLog.e("--------------------------------------------------------------------");

                        marginInfo.rightMargin = rightMargin + rectF.width() / 2;
                        marginInfo.bottomMargin = bottomMargin + rectF.height();

                        ZLog.e("2. " + marginInfo.leftMargin + "  :  " + marginInfo.topMargin);
                    }
                });
        highLight.show();
    }

}
